<template>
  <div class="user-list">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>用户基本信息管理</span>
          <el-button type="primary" @click="showAddUserForm">新增用户</el-button>
        </div>
      </template>

      <!-- 搜索框 -->
      <div class="search-section">
        <el-form :inline="true" class="search-form">
          <el-form-item label="真实姓名">
            <el-input v-model="searchName" placeholder="请输入真实姓名" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchByFullName">查询</el-button>
            <el-button @click="fetchUsers">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 用户列表 -->
      <el-table :data="users" style="width: 100%" border stripe>
        <el-table-column prop="userID" label="ID" width="80" />
        <el-table-column prop="username" label="用户名" width="120" />
        <el-table-column prop="fullName" label="真实姓名" width="120" />
        <el-table-column prop="role" label="角色" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.role === 'admin' ? 'danger' : 'success'">
              {{ scope.row.role === 'admin' ? '管理员' : '普通用户' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="email" label="邮箱" />
        <el-table-column prop="phone" label="电话" width="120" />
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="scope">
            <el-button type="primary" link @click="editUser(scope.row)">编辑</el-button>
            <el-button type="danger" link @click="deleteUser(scope.row.userID)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 表单弹窗 -->
    <el-dialog
      :title="selectedUser ? '编辑用户' : '新增用户'"
      v-model="showForm"
      width="50%"
      :close-on-click-modal="false"
    >
      <UserForm
        :user="selectedUser"
        @close="closeForm"
        @save="fetchUsers"
      />
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import UserForm from "./UserForm.vue";

export default {
  name: "UserList",
  components: { UserForm },
  data() {
    return {
      users: [], // 用户列表
      showForm: false, // 控制表单弹窗显示
      selectedUser: null, // 当前选中的用户
      searchName: "", // 搜索框输入内容
    };
  },
  methods: {
    // 获取用户列表
    fetchUsers() {
      axios
        .get("/user/list")
        .then((response) => {
          this.users = response.data;
        })
        .catch((error) => {
          console.error("获取用户列表失败:", error);
        });
    },

    // 根据真实姓名查询用户
    searchByFullName() {
      if (!this.searchName.trim()) {
        alert("请输入真实姓名！");
        return;
      }
      axios
        .get("/user/searchByFullName", {
          params: { fullName: this.searchName },
        })
        .then((response) => {
          this.users = response.data;
        })
        .catch((error) => {
          if (error.response && error.response.status === 404) {
            alert("未找到匹配的用户！");
          } else {
            console.error("查询用户失败:", error);
          }
        });
    },

    // 显示新增用户表单
    showAddUserForm() {
      this.selectedUser = null;
      this.showForm = true;
    },

    // 编辑用户
    editUser(user) {
      this.selectedUser = { ...user }; // 克隆对象，防止直接修改
      this.showForm = true;
    },

    // 删除用户
    deleteUser(userID) {
      if (confirm("确定要删除此用户吗？")) {
        axios
          .delete(`/user/delete/${userID}`)
          .then(() => {
            alert("删除成功！");
            this.fetchUsers();
          })
          .catch((error) => {
            console.error("删除用户失败:", error);
          });
      }
    },

    // 关闭表单
    closeForm() {
      this.showForm = false;
    },
  },
  mounted() {
    this.fetchUsers();
  },
};
</script>

<style scoped>
.user-list {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-section {
  margin-bottom: 20px;
  padding: 18px 0;
}

.search-form {
  display: flex;
  align-items: center;
}
</style>
